//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SpreadOption.h"
using namespace Cephei::QL::Experimental::Exoticoptions;
#include <gen/QL/Instruments/PlainVanillaPayoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/MultiAssetOption.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : CMultiAssetOption(CSpreadOption::typeid)
{
    CPlainVanillaPayoff^ _Cpayoff;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phSpreadOption = NULL;
#endif
        _Cpayoff = safe_cast<CPlainVanillaPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::PlainVanillaPayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::PlainVanillaPayoff>&> (_Cpayoff->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppSpreadOption = new boost::shared_ptr<QuantLib::SpreadOption> (new QuantLib::SpreadOption ( _payoff,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppSpreadOption)->setPricingEngine (_QL_Pricer);
        SetMultiAssetOption (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppSpreadOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (boost::shared_ptr<QuantLib::SpreadOption>& childNative, Object^ owner) : CMultiAssetOption(CSpreadOption::typeid)
{
#ifdef HANDLE
	_phSpreadOption = NULL;
#endif
	_ppSpreadOption = &childNative;
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppSpreadOption));
}
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (QuantLib::SpreadOption& childNative, Object^ owner) : CMultiAssetOption(CSpreadOption::typeid)
{
#ifdef HANDLE
	_phSpreadOption = NULL;
#endif
	_ppSpreadOption = new boost::shared_ptr<QuantLib::SpreadOption> (&childNative);
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppSpreadOption));
    _SpreadOptionOwner = owner;
    _MultiAssetOptionOwner = owner;
}

Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (CSpreadOption^ copy) : CMultiAssetOption(CSpreadOption::typeid)
{
#ifdef HANDLE
	_phSpreadOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSpreadOption = new boost::shared_ptr<QuantLib::SpreadOption> (copy->GetShared());
        _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppSpreadOption));
    }
}
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (PLATFORM::Type^ t) : CMultiAssetOption(CSpreadOption::typeid)
{
#ifdef HANDLE
	_phSpreadOption = NULL;
#endif
	if (!t->IsSubclassOf(CSpreadOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (QuantLib::Handle<QuantLib::SpreadOption>& childNative, Object^ owner)  : CMultiAssetOption(CSpreadOption::typeid)
{
	_phSpreadOption = &childNative;
	_ppSpreadOption = &static_cast<boost::shared_ptr<QuantLib::SpreadOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppSpreadOption));
    _SpreadOptionOwner = owner;
}
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (QuantLib::Handle<QuantLib::SpreadOption> childNative)  : CMultiAssetOption(CSpreadOption::typeid)
{
	_phSpreadOption = &childNative;
	_ppSpreadOption = &static_cast<boost::shared_ptr<QuantLib::SpreadOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppSpreadOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::CSpreadOption (QuantLib::SpreadOption childNative)  : CMultiAssetOption(CSpreadOption::typeid)
{
#ifdef HANDLE
	_phSpreadOption = NULL;
#endif
	_ppSpreadOption = new boost::shared_ptr<QuantLib::SpreadOption> (new QuantLib::SpreadOption (childNative));
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppSpreadOption));
}
#endif

Cephei::QL::Experimental::Exoticoptions::CSpreadOption::~CSpreadOption ()
{
    if (_ppSpreadOption != NULL)
    {
	    delete _ppSpreadOption;
        _ppSpreadOption = NULL;
    }
}
Cephei::QL::Experimental::Exoticoptions::CSpreadOption::!CSpreadOption ()
{
    if (_ppSpreadOption != NULL)
    {
	    delete _ppSpreadOption;
    }
}
QuantLib::SpreadOption& Cephei::QL::Experimental::Exoticoptions::CSpreadOption::GetReference ()
{
    if (_ppSpreadOption == NULL) throw REFNEW NativeNullException ();
	return **_ppSpreadOption;
}
boost::shared_ptr<QuantLib::SpreadOption>& Cephei::QL::Experimental::Exoticoptions::CSpreadOption::GetShared ()
{
    if (_ppSpreadOption == NULL) throw REFNEW NativeNullException ();
	return *_ppSpreadOption;
}
QuantLib::SpreadOption* Cephei::QL::Experimental::Exoticoptions::CSpreadOption::GetPointer ()
{
    if (_ppSpreadOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppSpreadOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SpreadOption>& Cephei::QL::Experimental::Exoticoptions::CSpreadOption::GetHandle ()
{
	if (_phSpreadOption == NULL)
	{
		_phSpreadOption = new Handle<QuantLib::SpreadOption> (*_ppSpreadOption);
	}
	return *_phSpreadOption;
}
#endif
bool Cephei::QL::Experimental::Exoticoptions::CSpreadOption::HasNative () 
{
	return (_ppSpreadOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Exoticoptions::ISpreadOption^ Cephei::QL::Experimental::Exoticoptions::CSpreadOption_Factory::Create (Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CSpreadOption ( payoff,  exercise,  QL_Pricer);
}
